package uicgas.pump.impl;

import javax.activation.UnsupportedDataTypeException;

import org.apache.log4j.Logger;

import uicgas.communication.RemoteCommand;
import uicgas.communication.impl.AbstractCommunicationImpl;
import uicgas.globaldata.ClientTypes;
import uicgas.globaldata.ControlMessage;
import uicgas.globaldata.Messages;
import uicgas.pump.PumpLogic;
import uicgas.pump.commands.ActivatePump;
import uicgas.pump.commands.AnnounceState;
import uicgas.pump.commands.AnnounceTransaction;
import uicgas.pump.commands.DeactivatePump;
import uicgas.pump.commands.DecommissionPump;
import uicgas.pump.commands.NotifyCardInvalid;
import uicgas.pump.commands.RecommissionPump;
import uicgas.tools.LogHelper;
import uicgas.tools.UicGasGeneralException;

public class CommunicationImpl extends AbstractCommunicationImpl
{
	private static final Logger LOGGER = LogHelper
			.getLogger(CommunicationImpl.class);

	private PumpLogic pumpLogic = null;

	//	private ObjectInputStream inputStreamAsClient = null;
	//	private ObjectOutputStream outputStreamAsClient = null;

	public CommunicationImpl(PumpLogic pumpLogic)
	{
		super(ClientTypes.PUMP);
		this.pumpLogic = pumpLogic;
	}

	/**
	 * {@inheritDoc}
	 */
	protected void processControlMessageFromServer(ControlMessage messageIn)
	{
		Messages message = messageIn.getMessage();
		if (LOGGER.isDebugEnabled())
		{
			LOGGER.debug(LogHelper.IN_COMING + "Recieved message[" + message
					+ "]. ");
		}

		RemoteCommand command = null;
		/*if (message == Messages.ANNOUNCESELF)
		{
			new AnnounceSelf(pumpLogic, this);
		}
		else*/
		if (message == Messages.VALID)
		{
			command = new ActivatePump(pumpLogic, this, null);
		}
		else if (message == Messages.INVALID)
		{
			command = new NotifyCardInvalid(pumpLogic, this);
		}
		else if (message == Messages.ACTIVATEPUMP)
		{
			command = new ActivatePump(pumpLogic, this, messageIn.getPayload());
		}
		else if (message == Messages.DEACTIVATEPUMP)
		{
			command = new DeactivatePump(pumpLogic, this);
		}
		else if (message == Messages.DECOMMISSIONPUMP)
		{
			command = new DecommissionPump(pumpLogic, this);
		}
		else if (message == Messages.RECOMMISSIONPUMP)
		{
			command = new RecommissionPump(pumpLogic, this);
		}
		else if (message == Messages.ANNOUNCESTATE)
		{
			command = new AnnounceState(pumpLogic, this, messageIn.getPayload());
		}
		else if (message == Messages.ANNOUNCETRANSACTION)
		{
			command = new AnnounceTransaction(pumpLogic, this,
					messageIn.getPayload());
		}
		else
		{
			throw new UicGasGeneralException(new UnsupportedDataTypeException(
					message.toString()));
		}

		executeCommand(command);

	}

	private void executeCommand(RemoteCommand command)
	{
		if (LOGGER.isDebugEnabled())
		{
			LOGGER.debug(LogHelper.IN_COMING + "Begin to process message["
					+ command + "]. ");
		}

		command.executeRemoteCommand();

		if (LOGGER.isDebugEnabled())
		{
			LOGGER.debug(LogHelper.IN_COMING + "END to process message["
					+ command + "]. ");
		}
	}

	@Override
	/**
	 * {@inheritDoc}
	 */
	protected int generateUniqueComponentID()
	{
		pumpLogic.generateNewID();
		return pumpLogic.getID();
	}

	protected void sucessfullyRegistered(int id)
	{
		pumpLogic.setShowingID(id);
	}

	//	@Override
	//	public ControlMessage sendValidateData(ControlMessage messageOut)
	//	{
	//		sendData(messageOut);
	//		return readData();
	//	}

	/*private ControlMessage readData()
	{
		ControlMessage messageBack = null;
		try
		{
			messageBack = (ControlMessage) inputStreamAsClient.readObject();
		}
		catch (IOException e)
		{
			throw new UicGasGeneralException(e);
		}
		catch (ClassNotFoundException e)
		{
			throw new UicGasGeneralException(e);
		}

		return messageBack;
	}*/

}
